package xuegao.study.mall.admin.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.admin.common.CommonPage;
import xuegao.study.mall.repo.dao.pms.PmsBrandDao;
import xuegao.study.mall.repo.dao.pms.PmsProductAttributeCategoryDao;
import xuegao.study.mall.repo.dao.pms.PmsProductAttributeDao;
import xuegao.study.mall.repo.dao.pms.PmsProductCategoryDao;
import xuegao.study.mall.admin.mapper.PmsProductAttributeMapper;
import xuegao.study.mall.admin.mapper.PmsProductCategoryMapper;
import xuegao.study.mall.common.model.pms.PmsBrand;
import xuegao.study.mall.common.model.pms.PmsProductAttribute;
import xuegao.study.mall.common.model.pms.PmsProductAttributeCategory;
import xuegao.study.mall.common.model.pms.PmsProductCategory;
import xuegao.study.mall.common.param.pms.PmsBrandParam;
import xuegao.study.mall.common.param.pms.PmsDeleteParam;
import xuegao.study.mall.admin.service.pms.PmsBrandService;
import xuegao.study.mall.common.vo.pms.PmsProductAttributeVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 品牌管理
 */
@RestController
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    private PmsBrandDao pmsBrandDao;

    @Autowired
    private PmsProductAttributeCategoryDao pmsProductAttributeCategoryDao;

    @Autowired
    private PmsProductAttributeDao pmsProductAttributeDao;

    @Autowired
    private PmsProductCategoryDao pmsProductCategoryDao;

    @Autowired
    private PmsProductAttributeMapper pmsProductAttributeMapper;

    @Autowired
    private PmsProductCategoryMapper pmsProductCategoryMapper;

    @Autowired
    private PmsBrandService pmsBrandService;

    @ApiOperation("商品品牌列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<PmsBrand>> queryPmsBrandList(@RequestParam(value = "keyword", required = false) String keyword,
                                                                @RequestParam(value = "showStatus", required = false) Integer showStatus,
                                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<PmsBrand> brandList = pmsBrandService.listBrand(keyword, showStatus, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(brandList));
    }

    @ApiOperation("商品品牌详情")
    @GetMapping("/{id}")
    public CommonResult<PmsBrand> getBrand(@PathVariable("id") Long id) {
        PmsBrand pmsBrand = pmsBrandDao.selectById(id);
        return CommonResult.success(pmsBrand);
    }

    @ApiOperation("添加商品品牌")
    @PostMapping("/create")
    public CommonResult addPmsBrand(@RequestBody PmsBrand pmsBrand) {
        pmsBrand.setProductCount(0);
        pmsBrand.setProductCommentCount(0);
        int count = pmsBrandDao.insert(pmsBrand);
        return CommonResult.success(count);
    }

    @ApiOperation("商品品牌编辑")
    @PostMapping("/update/{id}")
    public CommonResult<Integer> updateBrand(@PathVariable("id") Long id, @RequestBody PmsBrandParam param) {
        int count = pmsBrandService.update(id, param);
        if (count == 1) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("删除商品品牌")
    @GetMapping("/delete/{id}")
    public CommonResult<Integer> deleteBrand(@PathVariable("id") Long id) {
        int count = pmsBrandDao.deleteById(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }



    /**
     * 商品属性分类列表查询
     * @return
     */
    @GetMapping("/queryPmsProductAttributeCategoryList")
    public List<PmsProductAttributeCategory> queryPmsProductAttributeCategoryList() {
        Map<String, Object> params = new HashMap<>();
        List<PmsProductAttributeCategory> pmsProductAttributeCategories = pmsProductAttributeCategoryDao.selectByMap(params);
        return pmsProductAttributeCategories;
    }


    /**
     * 商品属性分类添加
     * @param pmsBrand
     * @return
     */
    @PostMapping("/addPmsProductAttributeCategory")
    public String addPmsProductAttributeCategory(@RequestBody PmsProductAttributeCategory pmsBrand) {
        pmsBrand.setAttributeCount(0);
        pmsBrand.setParamCount(0);
        pmsProductAttributeCategoryDao.insert(pmsBrand);
        return "ok";
    }

    /**
     * 商品属性列表查询
     * @return
     */
    @GetMapping("/queryPmsProductAttribute")
    public List<PmsProductAttributeVo> queryPmsProductAttribute() {
        Map<String, Object> params = new HashMap<>();
        List<PmsProductAttribute> pmsProductAttributes = pmsProductAttributeDao.selectByMap(params);
        List<PmsProductAttributeVo> voList = new ArrayList<>();
        if (pmsProductAttributes == null || pmsProductAttributes.size() == 0) {
            return voList;
        }
        // 查询属性分类
        List<Long> attributeCategoryIds = pmsProductAttributes.stream().map(PmsProductAttribute::getProductAttributeCategoryId).collect(Collectors.toList());
        List<PmsProductAttributeCategory> pmsProductAttributeCategories = pmsProductAttributeCategoryDao.selectBatchIds(attributeCategoryIds);
        Map<Long, List<PmsProductAttributeCategory>> attributeCategorieMap = pmsProductAttributeCategories.stream().collect(Collectors.groupingBy(PmsProductAttributeCategory::getId));
        for (PmsProductAttribute attribute : pmsProductAttributes) {
            PmsProductAttributeVo vo = pmsProductAttributeMapper.pmsProductAttribute2Vo(attribute);
            String name = attributeCategorieMap.get(vo.getProductAttributeCategoryId()).get(0).getName();
            vo.setProductAttributeCategoryName(name);
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 商品属性添加
     * @param pmsProductAttribute
     * @return
     */
    @PostMapping("/addPmsProductAttribute")
    public String addPmsProductAttribute(@RequestBody PmsProductAttribute pmsProductAttribute) {
        pmsProductAttributeDao.insert(pmsProductAttribute);
        return "ok";
    }

    /**
     * 该接口仅对新增商品分类开放
     * @return
     */
    @GetMapping("/queryPmsProductCategoryTopLevelList")
    public List<PmsProductCategory> queryPmsProductCategoryTopLevelList() {
        Map<String, Object> params = new HashMap<>();
        params.put("level", 0);
        params.put("parent_id", 0L);
        PmsProductCategory pmsProductCategory = new PmsProductCategory();
        pmsProductCategory.setId(0L);
        pmsProductCategory.setName("无上级分类");
        List<PmsProductCategory> resultList = pmsProductCategoryDao.selectByMap(params);
        if (resultList == null) {
            resultList = new ArrayList<>();
        }
        resultList.add(pmsProductCategory);
        return resultList;
    }

    /**
     * 商品分类添加
     * @param pmsProductCategory
     * @return
     */
    @PostMapping("/addPmsProductCategory")
    public String addPmsProductCategory(@RequestBody PmsProductCategory pmsProductCategory) {
        pmsProductCategory.setProductCount(0);
        if (pmsProductCategory.getParentId() == 0L) {
            pmsProductCategory.setLevel(0);
        } else {
            pmsProductCategory.setLevel(1);
        }
        pmsProductCategoryDao.insert(pmsProductCategory);
        return "ok";
    }

    @PostMapping("/delete")
    public String delete(@RequestBody PmsDeleteParam deleteParam) {
        Long id = deleteParam.getId();
        Integer type = deleteParam.getType();
        if (type == 1) {//删除商品分类
            pmsProductCategoryDao.deleteById(id);
        } else if (type == 2) {// 删除属性
            pmsProductAttributeDao.deleteById(id);
        } else if (type == 3) {// 删除属性分类
            pmsProductAttributeCategoryDao.deleteById(id);
        }
        return "ok";
    }
}
